package gw.com.android.ui.dialog;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import www.com.library.dialog.BasePopWindow;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.view.RecycleViewDivider;
import www.com.library.view.RecyclerClickListener;
import www.com.library.view.TintImageTextView;

/**
 * Created by reeta.zou on 2016/10/8.
 * 账号切换弹层
 */
public class AccoutChangePopWindow extends BasePopWindow {
    private DataItemResult mResult;
    private TextView mLoginView;
    private String curName = "";

    public AccoutChangePopWindow(Activity activity, View view, String curName, RecyclerClickListener callback) {
        this.curName = curName;
        initPopWindow(activity, view, R.layout.dialog_account_change,callback);
        // 高度自适应
        popWindow.setTouchable(true);
        popWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
    }

    @Override
    public void createPopWindow(View dialogView) {
        setArrayData();

        popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
        modeListAdapter = new AccountAdapter(activity);
        // 线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        // 设置布局管理器
        popListView.setLayoutManager(layoutManager);
        popListView.addItemDecoration(new RecycleViewDivider(activity, LinearLayoutManager.VERTICAL, false));

        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hidden();
                popWindow.setFocusable(false);
                popWindow.update();
            }
        });
        mLoginView = (TextView) dialogView.findViewById(R.id.login_btn);

        if(mResult.getDataCount() < 1){
            mLoginView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hidden();
                    popWindow.setFocusable(false);
                    popWindow.update();
                    GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_REAL);
                    ActivityManager.backLogin(activity, false);
                }
            });
            mLoginView.setVisibility(View.VISIBLE);
            popListView.setVisibility(View.GONE);
        }else{
            popListView.setAdapter(modeListAdapter);
            mLoginView.setVisibility(View.GONE);
            popListView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置界面数据
     */
    private void setArrayData(){
        mResult = new DataItemResult();
        ArrayList<String> mLoginList = GTConfig.instance().getLoginList();
        DataItemDetail item = null;
        String password = "";

        if(null != mLoginList && mLoginList.size() > 0){
            for (int i = 0; i < mLoginList.size(); i++){
                item = new DataItemDetail();
                password = GTConfig.instance().getLoginPass(mLoginList.get(i));
                if(!curName.equals(mLoginList.get(i)) && null != password && password.length() > 0){
                    item.setStringValue("name", mLoginList.get(i));
                    item.setStringValue("password", password);
//                    item.setIntValue("type",GTConfig.ACC_TYPE_REAL);
                    item.setStringValue("title",mLoginList.get(i));
                    item.setIntValue("iconID", R.mipmap.a_profiles_small);
                    mResult.addItem(item);
                }
            }
        }

//        mLoginList = GTConfig.instance().getLoginList(GTConfig.ACC_TYPE_DEMO);
//        if(null != mLoginList && mLoginList.size() > 0){
//            for (int i = 0; i < mLoginList.size(); i++){
//                item = new DataItemDetail();
//                password = GTConfig.instance().getLoginPass(GTConfig.ACC_TYPE_DEMO,mLoginList.get(i));
//                if(!curName.equals(mLoginList.get(i)) && null != password && password.length() > 0){
//                    item.setStringValue("name", mLoginList.get(i));
//                    item.setStringValue("password", password);
//                    item.setIntValue("type",GTConfig.ACC_TYPE_DEMO);
//                    item.setStringValue("title", AppMain.getAppString(R.string.system_account_change_demo, mLoginList.get(i)));
//                    item.setIntValue("iconID", R.mipmap.a_profiles_small);
//                    mResult.addItem(item);
//                }
//            }
//        }

    }

    @Override
    public void show() {
        if (hasShowing()) {
            return;
        }
        if (!activity.isFinishing()) {
            view = activity.getWindow().getDecorView();
            popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(activity,0.7f);
        }
    }

    /**
     * 分享类型列表适配器
     * @author reeta
     *
     */
    public class AccountAdapter extends RecyclerView.Adapter{
        private LayoutInflater mInflater;

        public AccountAdapter(Activity act) {
            mInflater = LayoutInflater.from(act);
        }

        public DataItemDetail getItem(int position) {
            if (position < 0 || position >= mResult.getDataCount())
                return null;
            return mResult.getItem(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = mInflater.inflate(R.layout.list_item_account_change, parent,false);
            return new ItemView(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ItemView viewHold = (ItemView) holder;
            if (viewHold.mView != null) {
                DataItemDetail mItem = getItem(position);
                String title = mItem.getString("title");
                if(DeviceUtil.instance().isPhone(title)){
                    viewHold.mView.setText(title);
                }else{
//                    if(title.startsWith(AppMain.getAppString(R.string.PREFIX_REAL_ACCOUNT))){
                    if(title.startsWith(ConfigUtil.instance().mConfigObject.optString(ConfigType.PREFIX_REAL_ACCOUNT))){

                        viewHold.mView.setText(AppMain.getAppString(R.string.system_account_change_real, title));
                    }else if(title.startsWith(ConfigUtil.instance().mConfigObject.optString(ConfigType.PREFIX_DEMO_ACCOUNT))){
                        viewHold.mView.setText(AppMain.getAppString(R.string.system_account_change_demo, title));
                    }
                }
                viewHold.mView.setImageResource(mItem.getInt("iconID"));
                viewHold.mView.setTag(position);
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if(null != mResult){
                return mResult.getDataCount();
            }
            return 0;
        }

        class ItemView extends RecyclerView.ViewHolder{

            @BindView(R.id.item_title)
            TintImageTextView mView;

            public ItemView(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            @OnClick(R.id.item_layout)
            void onClick() {
                if(CommonUtils.isFastDoubleClick()){
                    return;
                }
                if(null != callback){
                    int position = (int) mView.getTag();
                    hidden();
                    popWindow.setFocusable(false);
                    popWindow.update();
                    callback.onClick(position, getItem(position));
                }
            }
        }
    }
}
